<?php
/**
 * User: xzq
 * Date: 2018/8/6
 * Time: 11:59
 */

namespace app\wechat\home;
use app\common\controller\Common;
use app\wechat\model\Reservation as ReservationModel;
use app\wechat\model\Violation as ViolationModel;
use app\wechat\model\User as UserModel;
use app\wechat\model\Area as AreaModel;
use app\common\util\applet\Generate;
use think\Log;
//定时器任务
class Timing
{
    /**
     * 发送模板短信判断是否违规
     */
    public function remind(){
        log::init([
            'type'  => 'File',
            'path'  => APP_PATH.'logs/',
            'apart_level'   =>  ['remind']
        ]);
        $reservation = new ReservationModel();
        $violation = new ViolationModel();
        $area = new AreaModel();
        $generate = new Generate();
        $areaName = $area->getAllAreaName();
        $time = time();
        //预约提醒
        $yyFind['startTime'] = ['<=',$time+30*60];
        $yyFind['yyRemind'] = 0;
        $yyList = $reservation->getTimeAll($yyFind);
        if (!empty($yyList)){
            $yyUpdateData =[];
            foreach ($yyList as $yyk => $yyVa){
                $addr = $areaName[$yyVa['areaid']].' 编号：'.$yyVa['seatNum'];
                $yySendRes = $generate->sendInfo(1,$yyVa['openid'],$yyVa['formid'],$yyVa['startTime'],$yyVa['endTime'],$addr);
                $yySendArr = json_decode($yySendRes,true);
                if ($yySendArr['errcode'] == 0){
                    $yyUpdateData[$yyk]['id']=$yyVa['id'];
                    $yyUpdateData[$yyk]['yyRemind']=1;
                }else{
                    Log::record('预约id：'.$yyVa['id'].'预约提醒发送失败','remind');
                }
            }
            $yyUpdateRes = $reservation->updateAllData($yyUpdateData);
        }
        //签到提醒
        $qdFind['startTime'] = ['<=',$time-5*60];
        $qdFind['qdRemind'] = 0;
        $qdFind['r.status'] = 0;
        $qdList = $reservation->getTimeAll($qdFind);
        if (!empty($qdList)){
            foreach ($qdList as $qdk => $qdVa){
                $addr = $areaName[$qdVa['areaid']].' 编号：'.$qdVa['seatNum'];
                $qdSendRes = $generate->sendInfo(2,$qdVa['openid'],$qdVa['formid'],$qdVa['startTime'],$qdVa['endTime'],$addr);
                $qdSendArr = json_decode($qdSendRes,true);
                if ($qdSendArr['errcode'] == 0){
                    $qdUpdateData[$qdk]['id']=$qdVa['id'];
                    $qdUpdateData[$qdk]['qdRemind']=1;
                }else{
                    Log::record('预约id：'.$qdVa['id'].'签到提醒发送失败','remind');
                }
            }
            $qdUpdateRes = $reservation->updateAllData($qdUpdateData);
        }
        $signoff = config('module_library.signoff');
        //签退提醒
        $qtFind['endTime'] = ['<=',$time];
        $qtFind['qtRemind'] = 0;
        $qtFind['r.status'] = 2;
        $qtList = $reservation->getTimeAll($qtFind);
        if (!empty($qtList)){
            if (empty($signoff)){
                //关闭签退 自动签退
                foreach ($qtList as $qtk => $qtVa){
                    $qtUpdateData[$qtk]['id'] = $qtVa['id'];
                    $qtUpdateData[$qtk]['status'] = 5;
                }
                $qtUpdateRes = $reservation->updateAllData($qtUpdateData);
            }else{
                //启动签退 发送签退提醒
                foreach ($qtList as $qtk => $qtVa){
                    $addr = $areaName[$qtVa['areaid']].' 编号：'.$qtVa['seatNum'];
                    $qtSendRes = $generate->sendInfo(3,$qtVa['openid'],$qtVa['formid'],$qtVa['startTime'],$qtVa['endTime'],$addr);
                    $qtSendArr = json_decode($qtSendRes,true);
                    if ($qtSendArr['errcode'] == 0){
                        $qtUpdateData[$qtk]['id']=$qtVa['id'];
                        $qtUpdateData[$qtk]['qtRemind']=1;
                    }else{
                        Log::record('预约id：'.$qtVa['id'].'签退提醒发送失败','remind');
                    }
                }
                $qtUpdateRes = $reservation->updateAllData($qtUpdateData);
            }
        }

        //查询判断违规
        $qdwyFind['startTime'] = ['<=',$time-20*60];
        $qdwyFind['status'] = 0;
        if (empty($signoff)){
            $wyList = $reservation->getViolationAll($qdwyFind,[]);
        }else{
            $qtwyFind['endTime'] = ['<=',$time-10*60];
            $qtwyFind['status'] = 2;
            $wyList = $reservation->getViolationAll($qdwyFind,$qtwyFind);
        }
        if (!empty($wyList)){
            foreach ($wyList as $wyk => $wyVa){
                $wyUpdateData[$wyk]['id'] = $wyVa['id'];
                $wyUpdateData[$wyk]['status'] = 3;
                $wyAddData[$wyk]['reservationid'] = $wyVa['id'];
                $wyAddData[$wyk]['userid'] = $wyVa['userid'];
                if ($wyVa['status'] == 0){
                    $wyAddData[$wyk]['type'] = 1;
                }else{
                    $wyAddData[$wyk]['type'] = 2;
                }
                $wyAddData[$wyk]['status'] = 0;
            }
            $reservation->updateAllData($wyUpdateData);
            $violation->updateAllData($wyAddData);
        }
        //添加违规记录
        if (!empty($wyList)){
            $wgNum = config('module_library.violation_num');
            $wgRes = $violation->getGroupCount($wgNum);
            if($wgRes){
                $userUpdateData = [];
                foreach ($wgRes as $key => $value){
                    $userUpdateData[$key]['id'] = $value['userid'];
                    $userUpdateData[$key]['status'] = 0;
                }
                $user = new UserModel();
                $user->updateAllData($userUpdateData);
            }
        }
        return;
    }
}